package com.controller;


import com.domain.Function;
import com.github.pagehelper.PageInfo;
import com.service.FunctionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

@Controller
@RequestMapping("/function")
public class FunctionController {

    @Autowired
    private FunctionService functionService;

    @RequestMapping("/list/{pageNumber}")
    public String selectAllPage(@PathVariable(value = "pageNumber") Integer pageNumber,
                                @RequestParam(value = "pageSize", required = false, defaultValue = "5") Integer pageSize, ModelMap map,
                                @RequestParam(value = "valueId", required = false) Integer valueId) {
        PageInfo<Function> pageInfo = functionService.selectAll(pageNumber, pageSize);
        map.put("pageInfo", pageInfo);
        return "funList";
    }

    @RequestMapping("/queryListOne/{pageNumber}")
    public String queryListOne(@PathVariable(value = "pageNumber") Integer pageNumber,
                               @RequestParam(value = "pageSize", required = false, defaultValue = "5") Integer pageSize, ModelMap map,
                               @RequestParam(value = "valueId", required = false) Integer valueId) {

        PageInfo<Function> pageInfo = functionService.queryByOne(pageNumber, pageSize);
        map.put("pageInfo", pageInfo);
        return "funList";
    }

    @RequestMapping("/queryListZero/{pageNumber}")
    public String queryListZero(@PathVariable(value = "pageNumber") Integer pageNumber,
                                @RequestParam(value = "pageSize", required = false, defaultValue = "5") Integer pageSize, ModelMap map,
                                @RequestParam(value = "valueId", required = false) Integer valueId) {


        PageInfo<Function> pageInfo = functionService.queryByZero(pageNumber, pageSize);
        map.put("pageInfo", pageInfo);
        return "funList";
    }

    @GetMapping("/delete")
    public String delete(int id) {
        functionService.delete(id);
        return "redirect:/function/list/1";
    }

    @RequestMapping("/addUI")
    public String addUI() {
        return "functionAdd";
    }

    @PostMapping(value = "insert")
    public String insert(Function function) {
        functionService.insert(function);
        return "redirect:/function/list/1";
    }

    @RequestMapping("/updateUI")
    public ModelAndView updateUI(ModelAndView modelAndView, int id) {

        Function function = functionService.selectById(id);
        modelAndView.addObject("function", function);
        modelAndView.setViewName("functionUpdate");
        return modelAndView;
    }

//    @RequestMapping("/detail")
//    public ModelAndView detail(ModelAndView modelAndView, int id) {
//
//        Article article = articleService.selectById(id);
//        modelAndView.addObject("articleById", article);
//
//        List<ArticleType> typeList = articleTypeService.selectAll();
//        modelAndView.addObject("typeList", typeList);
//        modelAndView.setViewName("detail");
//        return modelAndView;
//    }

    @PostMapping("/update")
    public String update(Function function) {
        functionService.update(function);
        return "redirect:/function/list/1";
    }

    @RequestMapping("/skip")
    public String skip(int id) {
        if (id == 1) {
            return "redirect:/user/list/1";
        } else if (id == 2) {
            return "redirect:/role/list/1";
        } else if (id == 3) {
            return "redirect:/article/list/1";
        } else if (id == 4) {
            return "redirect:/type/typeListUI";
        }
        return "redirect:/function/list/1";
    }

}
